package com.qiwo.common.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.common.domain.UserInfoDO;
import com.qiwo.common.service.UserService;
import com.qiwo.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 
 * <pre>
 * 用户信息表
 *  @author  chenwenchao
 *  @date 2018-10-04 17:32:48
 * </pre>
 * <small> 2018-10-04 17:32:48 | chenwenchao</small>
 */
@Controller
@RequestMapping("/common/user")
public class UserInfoController extends AdminBaseController {
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("common:user:user")
	String User(){
	    return "common/user/user";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("common:user:user")
	public Result<Page<UserInfoDO>> list(UserInfoDO userDTO){
        Wrapper<UserInfoDO> wrapper = new EntityWrapper<UserInfoDO>().orderBy("id", false);
        Page<UserInfoDO> page = userService.selectPage(getPage(UserInfoDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("common:user:add")
	String add(){
	    return "common/user/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:user:edit")
	String edit(@PathVariable("id") Integer id, Model model){
		UserInfoDO user = userService.selectById(id);
		model.addAttribute("user", user);
	    return "common/user/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:user:add")
	public Result<String> save( UserInfoDO user){
		userService.insert(user);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:user:edit")
	public Result<String>  update( UserInfoDO user){
		boolean update = userService.updateById(user);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("common:user:remove")
	public Result<String>  remove( Long id){
		userService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:user:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		userService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
